package com.heima.kafka.controller;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojos.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


/**
 * 生产者
 */
@RestController
public class HelloController {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @GetMapping("/hello")
    public String hello(){
//        Map map =new HashMap();
//        map.put("name","西欧奥利");
//        kafkaTemplate.send("itcast-topic",JSON.toJSONString(map));
//        return "ok";
        for (int i = 0; i < 5; i++) {
            if(i%2==0){
                kafkaTemplate.send("boot-kafkastream-topic1","hello boot kafka");
            }else{
                kafkaTemplate.send("boot-kafkastream-topic1","hello boot itcast");
            }
        }
        return "发送成功";
    }
//
//    @GetMapping("/hello")
//    public String hello(){
//        User user = new User();
//       user.setName("小王");
//        user.setAge(18);
//
//        kafkaTemplate.send("user-topic", JSON.toJSONString(user));
//
//        return "ok";
//    }
}